
--保留技能
local ui = class.panel.create([[textures\black32.blp]],(1920-500)/2,200,500,400)
ui:add_text('替换英雄技能',0,10,nil,30,12,5)
ui:set_level(99)
ui:hide()

local target = ui:add_button([[ui\ui_b0.tga]],(ui.w-64)/2,60,64,64)
ui.icon = target:add_panel('')


local w1,h1 = 64,64
local x1 = 20
local y1 = 250
ui.buttons = {}

for a=1,7 do
    local panel = ui:add_button([[ui\ui_b0.tga]],x1 + (a-1)*65,y1,w1,h1)
    panel.icon = panel:add_panel('')
    panel.highlight1 = panel:add_highlight([[UI\Widgets\BattleNet\bnet-button01-highlight-mouse.blp]])
    panel.highlight1:hide()
    panel.index = a
    ui.buttons[a] = panel
end


ui.esc = ui:add_namebutton('取消',(ui.w-120)/2,330,120,50,16)

function ui:update()
    local locplayer = ac.player.self
    local hero = locplayer.hero
    if hero==nil then
        return
    end
    for a,panel in ipairs(ui.buttons) do
        local skill = hero:find_skill(a,'英雄')
        panel.skill = skill
        if skill then
            panel.icon:set_normal_image(skill.art)
        else
            panel.icon:set_normal_image('')
        end
    end
end


function ui:on_button_clicked(button)
    local skill = button.skill
    if button.index and skill then
        ac.sync('同步-替换技能',button.index)
        self:hide()
    elseif button==self.esc then
        ac.sync('同步-替换技能',0)
        self:hide()
    end
end

function ui:on_button_mouse_enter(button)
    local skill = button.skill
    if skill then
        local tip = format_obj(skill,skill.tip)
        self:tooltip(skill.title or skill.name,tip)
    end
end


ac.sync:event '同步-替换技能'(function(_,player,index)
    local hero = player.hero
    local skill = hero and hero:find_skill(index,'英雄')
    local name = player:get_data('替换技能')
    local lock = player:get_data('不会获取英雄') or {}
    player:set_data('替换技能',nil)
    if skill then
        skill:remove()
        hero:add_skill(name,'英雄',index)
        lock[skill.bind_hero] = nil
    end
end)

ac.game:event '玩家-显示替换技能'(function(_,player,name)
    local skill = ac.lni('skill',name)
    player:set_data('替换技能',name)
    if player:is_self() then
        ui.icon:set_normal_image(skill.art)
        ui.icon.skill = skill
        ui:update()
        ui:show()
    end
end)


ac.game:event '玩家-刷新UI'(function(_,player)
    if ui:is_show() then
        ui:update()
    end
end)